package com.ccit.controller;

import com.ccit.domain.Approval;
import com.ccit.domain.Employee;
import com.ccit.domain.PageBean;
import com.ccit.service.EmployeeService;
import com.ccit.utils.Code;
import com.ccit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/employees")
@CrossOrigin(origins = "http://localhost:8080")
@Api(description = "员工管理")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping
    @ApiOperation(value = "添加员工信息")
    public Result add(@RequestBody Employee employee) {
        boolean flag = employeeService.add(employee);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除 员工信息")
    public Result delete(@PathVariable Integer id) {
        boolean flag = employeeService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取 员工信息")
    public Result getById(@PathVariable Integer id) {
        Employee employee = employeeService.getById(id);
        Integer code = employee != null ? Code.GET_OK : Code.GET_ERR;
        String msg = employee != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,employee,msg);
    }

    @PutMapping
    public Result update(@RequestBody Employee employee) {
        boolean flag = employeeService.update(employee);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @GetMapping
    public Result selectAll(){
        List<Employee> employeesList = employeeService.selectAll();
        Integer code = employeesList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = employeesList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,employeesList,msg);
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页获取员工信息")
    public  Result getEmployees(@RequestParam("currentPage") int currentPage,
                                 @RequestParam("pageSize") int pageSize) {
        //2. 调用service查询
        PageBean<Employee> pageBean = employeeService.selectByPage(currentPage,pageSize);
        Integer code = pageBean != null ? Code.GET_OK : Code.GET_ERR;
        String msg = pageBean != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,pageBean,msg);
    }

    @PostMapping("/deleteByIds")
    public void deleteByIds(){

    }





}
